import api from '../../api'

export default {
    namespaced: true,
    state: {
        detail: {},
        commentList: [],
        total:0,
        pageSize:10,
        pageIndex:1,
        dialogVisible:false
    },
    mutations: {
        set_detail(state, payload) {
            state.detail = payload;
        },
        // 点赞
        add_likes(state, payload) {
            state.detail.likes++;
        },
        // 取消点赞
        down_likes(state, payload) {
            state.detail.likes--;
        },
        set_commentList(state, payload) {
            state.commentList = payload;
        },
        set_total(state, payload) {
            state.total = payload;
        },
        // 回复组件的显示隐藏
        set_reply(state,payload) {
            let {id,childId} = payload;
            let obj = state.commentList.find((item)=>item.id===id);
            if(childId) {
                obj = obj.children.find((item)=>item.id===childId);
            }
            if(obj.reply===0) {
                obj.reply = 1
            } else {
                obj.reply = 0
            }
        },
        // 切换分页
        set_pageIndex(state, payload) {
            state.pageIndex = payload;
        },
        // 分享框的显示隐藏
        set_dialogVisible(state, payload) {
            state.dialogVisible = payload;
        },
    },
    actions: {
        // 获取详情
        getArticleDetail(context, data) {
            api.getArticleDetail(data).then(res => {
                if (res.success) {
                    context.commit('set_detail', res.data)
                }
            })
        },
        // 获取一级评论列表
        getCommentList(context,data) {
            api.getFirstList(data).then((res) => {
                if(res.success){
                    context.commit("set_commentList",res.data)
                    context.commit("set_total",res.data.length)
                }
            });
        },
    },
}